<?php
require_once('data/SugarBean.php');
class JsFileGenerator {
	//generate Service Fee js file
	function JsFileGenerator(&$bean, $event, $arguments){
		$this->JsFileGenerators(&$bean, $event, $arguments,'China');	

		$this->JsFileGenerators(&$bean, $event, $arguments,'Japan');	
		$this->JsFileGenerators(&$bean, $event, $arguments,'Global');	

	}
	function JsFileGenerators(&$bean, $event, $arguments,$region){
		global $db;	
		$filePath="custom/modules/edanz_Service_fees/javascript/";
		$timestamp = time();
                $fileName="edanzfees.$region.js";
		$file_content="";
		$fees= $bean->get_full_list('',"business_unit = '$region' AND type<> 'QC' ");
		if ( $fees!= null ) {
			foreach ($fees as $fee) {
				$type = $fee->type;
				$fee_type= $fee->fee_type;
				$amount = str_replace(",","",strval($fee->fee));
				$scope = $fee->scope;
				
				if(empty($scope)){
					$concated_feename=$type.'_'.$fee_type.' = '.$amount.";";
					$file_content.="\r\n var ".strtoupper($concated_feename);
				}else{
					$concated_feename=$type.'_'.$fee_type.'_'.$scope.' = '.$amount.";";                   
					$file_content.="\r\n var ".strtoupper($concated_feename);
				}
				
			}
		}
	
                //generating javascript file for Job page
		$cascade_fees= $bean->get_full_list('',"business_unit = '$region'AND scope is not null AND type<> 'QC'");
		$counter = Array();	
		if ( $cascade_fees!= null ) {
                        foreach ($cascade_fees as $fee) {
				$type = $fee->type;
                                $fee_type = $fee->fee_type;
				$cascade_fees_spe= $bean->get_full_list('',"business_unit = '$region' AND scope is not null AND type<>'QC' AND type='$type' AND fee_type='$fee_type'");
				if($counter[$type.'_'.strtoupper($fee_type)]==1)continue;//1 stands for already added
				//$group_fees = "\r\n var ".$type.'_'.strtoupper($fee_type).' = ';
				foreach ($cascade_fees_spe as $fee) {
					$type = $fee->type;
					$fee_type= $fee->fee_type;
					$scope = $fee->scope;
					
				        $file_content.="\r\n var ".strtoupper($type.'_'.$fee_type.'_SWITCHER_'.$scope.' = '.($scope-1<0?$scope:$scope-1).';') ;                   

				}
				
				$counter[$type.'_'.strtoupper($fee_type)]=1;//1 stands for already added
				//$group_fees.='};';
				$file_content.=($group_fees);
                                
                        }
                }
	

			
		$file = fopen($filePath.$fileName, 'w') or die('Could not open file.');
		fwrite($file, $file_content) or die('Could not write to file.');
                fclose($file);


		// generate javascript file for tasks in R2  filename= edanzfees.tasks.[business_unit].js	
		$filePath="custom/modules/edanz_Service_fees/javascript/";
                $fileName="edanzfees.task.$region.js";
		$file_content="";
		$fees= $bean->get_full_list('',"business_unit = '$region' AND (type= 'QC' OR type='EDITOR')");
		if ( $fees!= null ) {
			foreach ($fees as $fee) {
				$type = $fee->type;
				$fee_type= $fee->fee_type;
				$amount = str_replace(",","",strval($fee->fee));
				$scope = $fee->scope;
				
				if(empty($scope)){
					$concated_feename=$type.'_'.$fee_type.' = '.$amount.";";
					$file_content.="\r\n var ".strtoupper($concated_feename);
				}else{
					$concated_feename=$type.'_'.$fee_type.'_'.$scope.' = '.$amount.";";                   
					$file_content.="\r\n var ".strtoupper($concated_feename);
				}
				
			}
		}
		
	
		$cascade_fees= $bean->get_full_list('',"business_unit = '$region'AND scope is not null AND type <> 'QC'");
		$counter = Array();	
		if ( $cascade_fees!= null ) {
                        foreach ($cascade_fees as $fee) {
				$type = $fee->type;
                                $fee_type = $fee->fee_type;
				$cascade_fees_spe= $bean->get_full_list('',"business_unit = '$region' AND scope is not null AND type<>'QC' AND type='$type' AND fee_type='$fee_type'");
				if($counter[$type.'_'.strtoupper($fee_type)]==1)continue;//1 stands for already added
				//$group_fees = "\r\n var ".$type.'_'.strtoupper($fee_type).' = ';
				foreach ($cascade_fees_spe as $fee) {
					$type = $fee->type;
					$fee_type= $fee->fee_type;
					$scope = $fee->scope;
					
				        $file_content.="\r\n var ".strtoupper($type.'_'.$fee_type.'_SWITCHER_'.$scope.' = '.($scope-1<0?$scope:$scope-1).';') ;                   

				}
				
				$counter[$type.'_'.strtoupper($fee_type)]=1;//1 stands for already added
				//$group_fees.='};';
				$file_content.=($group_fees);
                                
                        }
                }
	
		$file = fopen($filePath.$fileName, 'w') or die('Could not open file.');
		fwrite($file, $file_content) or die('Could not write to file.');
                fclose($file);
	
	}

}



?>
